<?php

namespace Caifenglei\AuthRbac\Http\Actions;

use Caifenglei\AuthRbac\Models\Role;
use Caifenglei\AuthRbac\Models\User;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth;

class UserAction
{
    public static function getUserClass()
    {
        return config('auth-rbac.models.user');
    }
    /**
     * 统一用户登录 - 自研版
     * @param $userInfo
     * @return array
     */
    public static function ssoUserLogin($userInfo)
    {
        $identify_type = $userInfo['identifiable_type'];
        //通过账号匹配用户或创建
        $account = $userInfo['username'];
        $sso_user_id = $userInfo['id'];
        $user = static::getUserClass()::firstOrCreate([
            'username' => $account,
            'identifiable_type' => $identify_type
        ], [
            'sso_id' => $sso_user_id,
            'name' => $userInfo['name'],
            'username' => $account,
            'password' => static::getUserClass()::getInitPassword($userInfo['username']),
            'password_status' => User::PASSWORD_INIT,
            'wx_oa_openid' => $userInfo['wx_oa_openid'],
            'wx_mp_openid' => $userInfo['wx_mp_openid'],
            'is_admin' => $identify_type === User::ID_ADMIN ? 1 : 0,
            'identifiable_type' => $identify_type,
            'org_id' => $userInfo['org_id'],
            'org_no' => $userInfo['org_no'],
        ]);

        //先期同步的数据，再更新sso user id
        if(!$user->sso_id){
            $user->sso_id = $sso_user_id;
            $user->save();
        }

        //登录系统获取token
        $token = Auth::guard('api')->login($user);
        $payload = auth('api')->payload();
        return [
            'user' => $user,
            "access_token" => $token,
            "token_type" => "bearer",
            "expires_in" => $payload->get('exp'),
        ];
    }

    /**
     * 统一用户登录 - 沐坤C30版
     * @param $userInfo
     * @return array
     */
    public static function c30SsoUserLogin($userInfo)
    {
        $user_type = strtolower($userInfo['user_type']);
        $identify_type = in_array($user_type, ['teacher', 'sys_user']) ? User::ID_TEACHER : User::ID_STUDENT;
        
        //通过账号匹配用户或创建
        $account = $userInfo['account'];
        $sso_user_id = $userInfo['only_id'];
        $user = static::getUserClass()::firstOrCreate([
            'username' => $account,
            'identifiable_type' => $identify_type
        ], [
            'sso_id' => $sso_user_id,
            'name' => $userInfo['real_name'],
            'username' => $account,
            'password' => static::getUserClass()::getInitPassword($account),
            'password_status' => User::PASSWORD_INIT,
            // 'wx_oa_openid' => $userInfo['wx_oa_openid'],
            // 'wx_mp_openid' => $userInfo['wx_mp_openid'],
            'is_admin' => 0,
            'identifiable_type' => $identify_type,
            'org_id' => 999, // $userInfo['org_id'],
            // 'org_no' => $userInfo['org_no'],
        ]);

        //先期同步的数据，再更新sso user id
        if(!$user->sso_id){
            $user->sso_id = $sso_user_id;
            $user->save();
        }

        //登录系统获取token
        $token = Auth::guard('api')->login($user);
        $payload = auth('api')->payload();
        return [
            'user' => $user,
            "access_token" => $token,
            "token_type" => "bearer",
            "expires_in" => $payload->get('exp'),
        ];
    }

    /**
     * 给用户分角色
     * @param $user
     * @param $roles
     */
    public static function assignRoles($user, $data)
    {
        $roles = Arr::get($data, 'roles');
        if(!is_null($roles)) {
            if(count($roles) > 0) {
                if (is_numeric($roles[0])) {
                    $roles = Role::whereIn('id', $roles)->pluck('name');
                }
                $user->syncRoles($roles);
            }else{
                $user->syncRoles([]);
            }
        }
    }
}
